package com.hgd.action;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.hgd.client.task.Task;
import com.hgd.client.task.TaskImplDelegate;
import com.hgd.client.task.TaskImplService;
import com.hgd.client.task.TaskKind;
import com.hgd.client.task.Waring;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class WaringAction extends ActionSupport{
	
	private List<TaskKind> taskKindList;
	private List<Waring> waringList;
	private Map<Integer,String> taskMap = new HashMap<Integer,String>();
	private Waring waring;
	private int addFlag;
	ActionContext context = ActionContext.getContext(); 
	//Map params = context.getParameters(); 
	TaskImplService implService = new TaskImplService();
	TaskImplDelegate taskDelegate = implService.getTaskImplPort();

	public String addWaring() throws Exception{
		String addResult = SUCCESS;
		//params = context.getParameters(); 
		//String[] id=(String[])params.get("adminId");
		//adminId = Integer.parseInt(id[0]);
		//System.out.println(waring.getTaskKind().getTkId());
		addFlag = taskDelegate.waringAdd(waring.getTaskKind().getTkId(),waring);
		if (addFlag==1) {
			addResult = SUCCESS;
		}else{
			addResult = ERROR;
		}
		return addResult;
	}
	
	public String waringMonitoring() throws Exception{
		//String waringResult = SUCCESS;
		//params = context.getParameters(); 
		//String[] id=(String[])params.get("adminId");
		//adminId = Integer.parseInt(id[0]);
		List<Task> taskList = taskDelegate.taskStatusMonitoring();
		//System.out.println(taskList.size());
		for (Iterator iterator = taskList.iterator(); iterator.hasNext();) {
			Task task = (Task) iterator.next();
			//System.out.println("task status:"+task.getDStatus());
			if (task.getDStatus()==3) {		//fault
				//System.out.println("task id:"+task.getTId());
				taskMap.put(task.getTId(),"fault");
			}
			else if(task.getDStatus()==2){		//waring
				taskMap.put(task.getTId(),"waring");
			}
			else if(task.getDStatus()==0){		//disconnect
				taskMap.put(task.getTId(),"disconnect");
			}		
		}
		return "map";
	}
	
	/**
	 * 获得任务类型
	 * @return
	 */
	public String getAllTaskKind(){	
		taskKindList = taskDelegate.getDeviceTaskKind();
		waringList  = taskDelegate.getWaringList();
		return SUCCESS;
	}
	
	public List<Waring> getWaringList() {
		return waringList;
	}
	public void setWaringList(List<Waring> waringList) {
		this.waringList = waringList;
	}
	public List<TaskKind> getTaskKindList() {
		return taskKindList;
	}
	public void setTaskKindList(List<TaskKind> taskKindList) {
		this.taskKindList = taskKindList;
	}
	public Waring getWaring() {
		return waring;
	}
	public void setWaring(Waring waring) {
		this.waring = waring;
	}
	public int getAddFlag() {
		return addFlag;
	}
	public void setAddFlag(int addFlag) {
		this.addFlag = addFlag;
	}


	public Map<Integer, String> getTaskMap() {
		return taskMap;
	}

	public void setTaskMap(Map<Integer, String> taskMap) {
		this.taskMap = taskMap;
	}

	public static void main(String[] args) throws Exception {
		WaringAction action = new WaringAction();
		action.waringMonitoring();
	}
}
